$(function() {
    var begin = 0;
    var limit = 20;
    var fetching = false;
    var hasMore = true;
    var normalTopicTemplate = _.template($("#normal-topic-template").html());
    var hotTopicTemplate = _.template($("#hot-topic-template").html());

    function renderHotTopic() {
        var hotTopicUrl = '/api/topic/hot';
        var hotTopicData = {
            postData: JSON.stringify({
                idx: 0,
                params: {
                    provid: 0
                }
            })
        };

        $.ajax({
            url: hotTopicUrl,
            type: 'post',
            dataType: 'json',
            data: hotTopicData,
            success: function(res) {
                if(res.ret == 0) {
                    var topicElem = $(".swiper-wrapper");
                    $.each(res.res, function(idx, hotTopic) {
                        topicElem.append(hotTopicTemplate(hotTopic));
                    });
                    initSlider();
                }
            },
            error: function(res) {
                console.log(res.msg)
            },
            complete: function() {
            }
        });
    }

    function renderNormalTopic() {
        if (fetching) {
            return;
        }
        fetching = true;

        var normalTopicUrl = '/api/topic/list';
        var normalTopicData = {
            postData: JSON.stringify({
                idx: 0,
                params: {
                    begin: begin,
                    limit: limit,
                    provid: 0
                }
            })
        };

        $.ajax({
            url: normalTopicUrl,
            type: 'post',
            dataType: 'json',
            data: normalTopicData,
            success: function(res) {
                if(res.ret == 0) {
                    var topicElem = $(".normal-topics");
                    if(begin == 0) {
                        topicElem.empty();
                    }
                    $.each(res.res, function(idx, normalTopic) {
                        topicElem.append(normalTopicTemplate(normalTopic));
                    });
                    begin += limit;
                    if(res.res.length < limit) {
                        $(".topic-more").show();
                        $(".topic-more-txt").html("已加载全部");
                        hasMore = false;
                    }
                }
            },
            error: function(res) {
                console.log(res.msg)
            },
            complete: function() {
                fetching = false;
            }
        });
    }

    function initSlider() {
        var swiper = new Swiper('.swiper-container', {
            pagination: '.swiper-pagination',
            paginationClickable: true,
            loop: true,
            autoplay: 3000,
            autoplayDisableOnInteraction: false
        });
    }

    var winH = $(window).height(); //页面可视区域高度
    $(window).scroll(function () {
        if(!hasMore) {
            return;
        }

        var pageH = $(document.body).height();
        var scrollT = $(window).scrollTop(); //滚动条top
        var aa = (pageH-winH-scrollT)/winH;
        if(aa < 0.02){
            renderNormalTopic();
        }
    });

    renderHotTopic();
    renderNormalTopic();
});
